<?php
/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class Application_Model_UserMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Users';
    }

    /**
     * Gebruiker met een bepaald e-mailadres ophalen
     *
     * @param string $email
     * @return Application_Model_User
     */
    public function fetchByEmail($email)
    {
        $table = $this->getDbTable();

        $select = $table->select();
        $select->where('user_email = :email')
               ->bind( array(':email' => $email) );

        #Zend_Debug::dump($select->__toString());
        $row = $table->fetchRow($select);
        #Zend_Debug::dump($row);

        return $this->_toUser($row);
    }
    
    /**
     * Gebruikers met een bepaald e-mailadres ophalen
     *
     * @param string $email
     * @return array
     */
    public function fetchAllByEmail($email)
    {
        $table = $this->getDbTable();

        $select = $table->select();
        $select->where('user_email LIKE :email')
               ->bind( array(':email' => "{$email}%") )
               ->order('user_email ASC')
               ->limit(10);

        #Zend_Debug::dump($select->__toString());
        $rows = $table->fetchAll($select);
        #Zend_Debug::dump($rows);

        return $this->_toUsers($rows);
    }
    
    /**
     * Gebruiker met een bepaalde id ophalen
     * 
     * @param integer $id
     * @return Application_Model_User
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('user_id = ?' => $id));

        return $this->_toUser($row);
    }

    /**
     * Alle Users ophalen
     * 
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_toUsers($rows);
    }

    /**
     * User opslaan
     *
     * @param Application_Model_User $user
     */
    public function save(Application_Model_User $user)
    {
        $data = array(
            'user_email'      => $user->Email,
            'user_password'   => $user->Password,
            'user_displayname'  => $user->Displayname,
        );

        if (null == $user->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('user_id = ?' => $user->Id));
        }
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_User 
     */
    private function _toUser(Zend_Db_Table_Row_Abstract $row = null)
    {
        $user = new Application_Model_User();

        if ($row) {
            $user->Id           = $row['user_id'];
            $user->Email        = $row['user_email'];
            $user->Displayname  = $row['user_displayname'];
            $user->WorldId      = $row['worlds_world_id'];
            $user->LevelId      = $row['levels_level_id'];            
        }

        return $user;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toUsers(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $users = array();

        if ($rows) {
            foreach ($rows as $row) {
                $users[] = $this->_toUser($row);
            }
        }
        
        return $users;
    }

}